package com.yanqu.road.dataHelper.crossServer;

import com.alibaba.druid.util.JdbcUtils;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class Base {

    protected static Logger logger = LogManager.getLogger(TempDao.class.getName());

    Connection connection;

    Connection logConnection;

    public Connection getConnection() {
        return connection;
    }

    public Connection getLogConnection() {
        return logConnection;
    }

    public static Logger getLogger() {
        return logger;
    }

    public void closeConnection(PreparedStatement pstmt, ResultSet rs) {
        JdbcUtils.close(rs);
//        closeConnection(pstmt);
    }

    private void closeConnection(PreparedStatement pstmt) {
        try {

            if (pstmt != null && pstmt.isClosed() == false) {

                pstmt.clearParameters();
            }
            if (connection.isClosed() == false) {

                if (connection.getAutoCommit() != true) {
                    connection.commit();
                    connection.setAutoCommit(true);
                }
            }

        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        } finally {
            JdbcUtils.close(pstmt);
            JdbcUtils.close(connection);
        }
    }

    private void prepareCommand(PreparedStatement pstmt, Map<Integer, DbParameter> parms) throws SQLException {
        if (parms == null)
            return;
        for (Map.Entry<Integer, DbParameter> entry : parms.entrySet()) {
            pstmt.setObject(entry.getKey(), entry.getValue().getResult());
        }
    }

    protected String checkNullToEmpty(String target){
        if(target == null){
            return "";
        }
        return target;
    }

    public int executeBatchQueryUser(String sqlText, List<Map<Integer, DbParameter>> params){
        return -1;
    }

    public int executeBatchQueryLog(String sqlText, List<Map<Integer, DbParameter>> params){
        return -1;
    }

    public int executeBatchQuery(String sqlText, List<Map<Integer, DbParameter>> params) {
        int count = 0;

        PreparedStatement pstmt = null;

        try {
            connection.setAutoCommit(false);
            pstmt = connection.prepareStatement(sqlText, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            for (Map<Integer, DbParameter> map : params) {
                prepareCommand(pstmt, map);
                pstmt.addBatch();
                count++;
                if (count % 1500 == 0) {
                    pstmt.executeBatch();
                    connection.commit();
                    count = 0;
                }
            }

            if (count > 0) {
                pstmt.executeBatch();
                connection.commit();
            }
            return 1;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        } finally {

            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                logger.error(e.getMessage(), e);
            }
            // watch.commit(sqlText);
        }
        return -1;
    }
}
